class HeadIcon extends BaseSprite {

    private group: eui.Group;
    private img_head: eui.Image;
    private img_offline: eui.Image;
    private vipGroup: eui.Group;
    private img_vip: eui.Image;
    private effect_img: eui.Rect;

    private img_kuang: eui.Image;
    private img_owner: eui.Image;
    private img_zhuang: eui.Image;
    private img_que: eui.Image;
    private img_ting: eui.Image;

    bar: AssessBar;
    private lab_nick: eui.Label;
    private lab_uid: eui.Label;
    private lab_fen: eui.Label;

    private btn_kill: eui.Button;

    private _isOwner: boolean = false;

    /**流光 容器 */
    private mc_group: eui.Group;

    player: PlayerVo;
    dir: Dir;

    constructor(dir: Dir = Dir.bottom) {
        super();
        this.skinName = "HeadIconSkin";
        this.dir = dir;
    }

    childrenCreated() {
        super.childrenCreated();

        if (GameConfig.isPureness) this.lab_uid.visible = false;

        this.clean();

        this.addEventListener(egret.TouchEvent.TOUCH_TAP, this.clickHandler, this);

        gameManager.i.addEventListener(GameEventType.TELPHONE_CONFIRM, this.telphoneHandler, this);

        gameManager.i.addEventListener(GameEventType.UPDATE_CHAT, this.chatHandler, this);
        gameManager.i.addEventListener(GameEventType.UPDATE_EXPRESSION, this.expressionHandler, this);
    }

    private expressionHandler(expression: any) {
        if (!this.player) return;
        if (!expression) return;
        if (expression.uid != this.player.uid) return;

        if (this.skinState == 'intable' || this.skinState == 'ingame') {
            let item: ChatContentExpression = new ChatContentExpression();
            item.update(expression, this.localToGlobal(0, 0).x + this.width / 2, this.localToGlobal(0, 0).y + this.height / 2);
        }
    }

    private chatHandler(chat: any) {
        if (!this.player) return;
        if (!chat) return;
        if (chat.uid != this.player.uid) return;

        if (this.skinState == 'intable' || this.skinState == 'ingame') {
            let item: ChatContentText = new ChatContentText();
            item.update(chat, this.localToGlobal(0, 0).x + this.width / 2, this.localToGlobal(0, 0).y);
        }
    }

    private telphoneHandler() {
        if (!this.player) return;
        if (this.player.uid != game.player.uid) return;

        this.isVip = true;
    }

    private clickHandler(e: egret.TouchEvent) {
        if (PublicVal.state == StateType.gameover || PublicVal.state == StateType.over || PublicVal.state == StateType.replay) {
            return;
        }

        switch (e.target) {
            case this.btn_kill:
                if (this.player) gameManager.socketManager.send(22, { pos: this.player.pos });
                break;
            default:
                let roleInfoPanel: RoleinfoPanel = gameManager.find('RoleinfoPanel');
                if (roleInfoPanel && this.player) {
                    roleInfoPanel.show();
                    roleInfoPanel.refreshRole(this.player, this.skinState == 'intable' || this.skinState == 'ingame');
                }
                break;
        }
    }

    update(player: PlayerVo) {

        if (!this.player) this.player = new PlayerVo();

        if (PublicVal.state == StateType.replay) { this.skinState = "replay" };

        this.player.update(player);

        if (player) {

            this.vipGroup.visible = GameConfig.switch_shoujirenzheng;

            this.isOffline = this.player.status == "offline";
            this.isOwner = this.player.pos == 1;
            this.isVip = this.player.isVip;
            this.src = this.player.pic;

            this.lab_nick.text = "" + this.player.nick;
            this.lab_uid.text = "ID:" + this.player.uid;

            this.bar.show();
            this.bar.update(player.cai, player.zan);

            if (PublicVal.state == StateType.ready) {
                this.btn_kill.visible = this.player && PublicVal.state == StateType.ready && game.isRoomOwner && this.player.pos != 1;
            }
            else {
                this.btn_kill.visible = false;
            }

            if (game.roomState == RoomState.CIRCLE) {
                this.btn_kill.visible = false;
            }

        }
        else {
            this.clean();
        }
    }

    reset() {
        this.isTing = false;
        this.clearEffect();
        this.clearFrame();
    }

    clean() {
        this.reset();

        this.player = null;
        this.src = 'nobody';

        this.vipGroup.visible = false;
        this.btn_kill.visible = false;

        this.isVip = false;
        this.isOwner = false;
        this.isZhuang = false;
        this.isOffline = false;

        this.lab_nick.text = "";
        this.lab_uid.text = "";
        this.lab_fen.text = "";
        this.img_que.source = "";

        this.bar.hide();

        this.btn_kill.visible = false;

    }

    //头像闪烁特效
    public setEffect() {
        if (this.effect_img.visible == false) {
            this.effect_img.visible = true;
            // EffectUtils.twinkle(this.effect_img, 1, 1000);
            egret.Tween.get(this.effect_img, { loop: true }).to({ alpha: 0.6 }, 500).to({ alpha: 0 }, 300);
        }
    }

    public clearEffect() {
        egret.Tween.removeTweens(this.effect_img);
        this.effect_img.visible = false;
    }
    /**
     * 头像流光
    */
    public setFrame() {
        MCMgr.i.play(MC.Key.HEAD, this.mc_group);
    }
    /**
     * 清理头像流光
    */
    public clearFrame() {
        if (this.mc_group.numChildren > 0) {
            this.mc_group.removeChildren();
        }
    }

    set src(source: any) {
        this.img_head.source = '';

        if (typeof source == "string") {
            if (RES.hasRes(String(source))) {
                this.img_head.source = source;
            }
            else if (source == '' || !source || source == 'undefined' || source == 'null') {
                this.img_head.source = "head_default";
            }
            else if (source == 'nobody') {
                this.img_head.source = "head_nobody";
            }
            else if (source.indexOf('http') == 0 || source.indexOf('https') == 0) {
                let thiz = this;
                RES.getResByUrl(source, function (t) {
                    thiz.img_head.source = t;
                }, this, RES.ResourceItem.TYPE_IMAGE);
            } else if (source == "assess") {
                this.img_head.source = "head_assess";
            } else if (source == "unassess") {
                this.img_head.source = "head_unassess";
            }
        }
        else {
            this.img_head.source = source;
        }
    }

    get score() {
        return +this.lab_fen.text;
    }

    set score(num: number) {
        this.lab_fen.text = "" + num;
    }

    set isOffline(va: boolean) {
        this.img_offline.visible = va;
    }

    set isZhuang(va: boolean) {
        this.img_zhuang.visible = va;
    }

    get isOwner() {
        return this._isOwner;
    }

    set isOwner(va: boolean) {
        this._isOwner = va;

        this.img_owner.visible = va;

        this.img_kuang.source = va && this.player && this.player.isVip ? 'head_kuang_vip' : 'head_kuang';
    }

    set isVip(va: boolean) {
        this.img_vip.source = va ? 'icon_vip1' : 'icon_vip2';
        this.isOwner = this.isOwner;
    }

    set isTing(va: boolean) {
        this.img_ting.horizontalCenter = this.dir == 3 ? -65 : 0;
        this.img_ting.verticalCenter = this.dir == 3 ? 0 : -55;
        this.img_ting.visible = va;
        this.img_ting.source = "ting_icon";
    }

    set isMinglou(va: boolean) {
        this.img_ting.horizontalCenter = this.dir == 3 ? -65 : 0;
        this.img_ting.verticalCenter = this.dir == 3 ? 0 : -55;
        this.img_ting.visible = va;
        this.img_ting.source = "minglou_icon";
    }
}
/**
 * 0 头像明暗闪烁
 * 1 流光效果
 */
enum HeadEffType {
    flash = 0,
    frame,
}